package com.lehecai.core.lottery.fetcher.jczq.impl.worker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lehecai.core.lottery.JczqSPType;
import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.jczq.AbstractJczqDynamicInstantSpFetchWorker;
import com.lehecai.core.lottery.fetcher.jczq.JczqDynamicInstantSpItem;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreHttpUtils;
import com.lehecai.core.util.CoreJSONUtils;

/**
 * 通用竞彩足球浮动奖金即时sp抓取实现类
 *
 */
public class CommonJczqDynamicInstantSpFetchWorkerDataAPI extends AbstractJczqDynamicInstantSpFetchWorker{
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	private static final String URL_PENGINE_API = "http://data.lecai.com/api/simple/sporttery/jczq/immediate/sp.do";

    @Override
	public List<JczqDynamicInstantSpItem> fetchJczqSp(String phaseNo, LotteryType lotteryType, String terminalId){
		if (StringUtils.isBlank(phaseNo)) {
			logger.error("竞彩足球抓取官方sp时，officialDate为空");
			return null;
		}
		phaseNo = phaseNo.replace("-", "");
		List<String> list = null;
		try {
            Map<String, String> requestMap = new HashMap<String, String>();
            requestMap.put("phaseNo", phaseNo);
            requestMap.put("l", String.valueOf(lotteryType.getValue()));
            requestMap.put("p", "1");
			list = CoreHttpUtils.getUrl(URL_PENGINE_API, requestMap, CharsetConstant.CHARSET_UTF8, 100000);
		} catch (Exception e) {
			logger.error("竞彩足球抓取官方浮动奖金即时sp失败", e);
			return null;
		}
		if (list == null || list.size() == 0) {
			logger.error("竞彩足球抓取官方浮动奖金即时sp失败");
			return null;
		}
		String matches = list.get(0);
		if (matches == null || "".equals(matches)) {
			logger.error("未获取到竞彩足球官方浮动奖金即时sp");
			return null;
		}
		
		JSONObject dataJson = null;
		try {
			dataJson = JSONObject.fromObject(matches);
		} catch (Exception e) {
			logger.error("竞彩足球抓取官方浮动奖金即时sp失败", e);
			return null;
		}
		
		if (dataJson == null) {
			logger.error("未获取到竞彩足球官方浮动奖金即时sp");
		}
		
		int code = dataJson.getInt("code");
		if(code == -1){
			logger.error("竞彩足球抓取官方浮动奖金即时spsp出错");
			return null;
		}
		
		JSONArray jsonArray = dataJson.getJSONArray("data");
		if (jsonArray == null) {
			logger.error("未获取到竞彩足球官方浮动奖金即时sp");
			return null;
		}
		List<JczqDynamicInstantSpItem> jczqDynamicInstantSpItems = new ArrayList<JczqDynamicInstantSpItem>();
		for (Iterator<?> iterator = jsonArray.iterator(); iterator.hasNext();) {
			JSONObject object = (JSONObject) iterator.next();
			if (object == null) {
				return null;
			}

            // 如果指定了彩期，忽略掉返回结果中的非指定彩期的结果
            if (StringUtils.isNotBlank(phaseNo)) {
                String fetchedPhase = CoreJSONUtils.getString(object, "od");
                if (!fetchedPhase.equals(phaseNo)) {
                    continue;
                }
            }

			JczqDynamicInstantSpItem item = new JczqDynamicInstantSpItem();
			
			item.setMatchNum(CoreJSONUtils.getString(object, "mnu"));
			item.setOfficialNum(CoreJSONUtils.getString(object, "on"));
			item.setOfficialDate(CoreJSONUtils.getDate(object, "od", "yyyyMMdd"));
			item.setMatchName(CoreJSONUtils.getString(object, "mn"));
			item.setMatchDate(CoreJSONUtils.getDate(object, "md", "yyyy-MM-dd HH:mm:ss"));
			item.setHomeTeam(CoreJSONUtils.getString(object, "ht"));
			item.setAwayTeam(CoreJSONUtils.getString(object, "at"));
			
			JSONObject spObj = object.getJSONObject("sp");
			
			Map<JczqSPType, String> map = new HashMap<JczqSPType, String>();
			if (lotteryType.getValue() == LotteryType.JCZQ_SPF.getValue()) {
				map.put(JczqSPType.SPF_S, CoreJSONUtils.getString(spObj, "w"));
				map.put(JczqSPType.SPF_P, CoreJSONUtils.getString(spObj, "d"));
				map.put(JczqSPType.SPF_F, CoreJSONUtils.getString(spObj, "l"));
			} else if (lotteryType.getValue() == LotteryType.JCZQ_BF.getValue()) {
				map.put(JczqSPType.BF_ZS_1_0, CoreJSONUtils.getString(spObj, "0100"));
				map.put(JczqSPType.BF_ZS_2_0, CoreJSONUtils.getString(spObj, "0200"));
				map.put(JczqSPType.BF_ZS_2_1, CoreJSONUtils.getString(spObj, "0201"));
				map.put(JczqSPType.BF_ZS_3_0, CoreJSONUtils.getString(spObj, "0300"));
				map.put(JczqSPType.BF_ZS_3_1, CoreJSONUtils.getString(spObj, "0301"));
				map.put(JczqSPType.BF_ZS_3_2, CoreJSONUtils.getString(spObj, "0302"));
				map.put(JczqSPType.BF_ZS_4_0, CoreJSONUtils.getString(spObj, "0400"));
				map.put(JczqSPType.BF_ZS_4_1, CoreJSONUtils.getString(spObj, "0401"));
				map.put(JczqSPType.BF_ZS_4_2, CoreJSONUtils.getString(spObj, "0402"));
				map.put(JczqSPType.BF_ZS_5_0, CoreJSONUtils.getString(spObj, "0500"));
				map.put(JczqSPType.BF_ZS_5_1, CoreJSONUtils.getString(spObj, "0501"));
				map.put(JczqSPType.BF_ZS_5_2, CoreJSONUtils.getString(spObj, "0502"));
				map.put(JczqSPType.BF_ZS_QT, CoreJSONUtils.getString(spObj, "ho"));
				map.put(JczqSPType.BF_ZP_0_0, CoreJSONUtils.getString(spObj, "0000"));
				map.put(JczqSPType.BF_ZP_1_1, CoreJSONUtils.getString(spObj, "0101"));
				map.put(JczqSPType.BF_ZP_2_2, CoreJSONUtils.getString(spObj, "0202"));
				map.put(JczqSPType.BF_ZP_3_3, CoreJSONUtils.getString(spObj, "0303"));
				map.put(JczqSPType.BF_ZP_QT, CoreJSONUtils.getString(spObj, "do"));
				map.put(JczqSPType.BF_ZF_0_1, CoreJSONUtils.getString(spObj, "0001"));
				map.put(JczqSPType.BF_ZF_0_2, CoreJSONUtils.getString(spObj, "0002"));
				map.put(JczqSPType.BF_ZF_1_2, CoreJSONUtils.getString(spObj, "0102"));
				map.put(JczqSPType.BF_ZF_0_3, CoreJSONUtils.getString(spObj, "0003"));
				map.put(JczqSPType.BF_ZF_1_3, CoreJSONUtils.getString(spObj, "0103"));
				map.put(JczqSPType.BF_ZF_2_3, CoreJSONUtils.getString(spObj, "0203"));
				map.put(JczqSPType.BF_ZF_0_4, CoreJSONUtils.getString(spObj, "0004"));
				map.put(JczqSPType.BF_ZF_1_4, CoreJSONUtils.getString(spObj, "0104"));
				map.put(JczqSPType.BF_ZF_2_4, CoreJSONUtils.getString(spObj, "0204"));
				map.put(JczqSPType.BF_ZF_0_5, CoreJSONUtils.getString(spObj, "0005"));
				map.put(JczqSPType.BF_ZF_1_5, CoreJSONUtils.getString(spObj, "0105"));
				map.put(JczqSPType.BF_ZF_2_5, CoreJSONUtils.getString(spObj, "0205"));
				map.put(JczqSPType.BF_ZF_QT, CoreJSONUtils.getString(spObj, "ao"));
			} else if (lotteryType.getValue() == LotteryType.JCZQ_JQS.getValue()) {
				map.put(JczqSPType.JQS_0, CoreJSONUtils.getString(spObj, "s0"));
				map.put(JczqSPType.JQS_1, CoreJSONUtils.getString(spObj, "s1"));
				map.put(JczqSPType.JQS_2, CoreJSONUtils.getString(spObj, "s2"));
				map.put(JczqSPType.JQS_3, CoreJSONUtils.getString(spObj, "s3"));
				map.put(JczqSPType.JQS_4, CoreJSONUtils.getString(spObj, "s4"));
				map.put(JczqSPType.JQS_5, CoreJSONUtils.getString(spObj, "s5"));
				map.put(JczqSPType.JQS_6, CoreJSONUtils.getString(spObj, "s6"));
				map.put(JczqSPType.JQS_7, CoreJSONUtils.getString(spObj, "s"));
			} else if (lotteryType.getValue() == LotteryType.JCZQ_BQC.getValue()) {
				map.put(JczqSPType.BQC_SS, CoreJSONUtils.getString(spObj, "hh"));
				map.put(JczqSPType.BQC_SP, CoreJSONUtils.getString(spObj, "hd"));
				map.put(JczqSPType.BQC_SF, CoreJSONUtils.getString(spObj, "ha"));
				map.put(JczqSPType.BQC_PS, CoreJSONUtils.getString(spObj, "dh"));
				map.put(JczqSPType.BQC_PP, CoreJSONUtils.getString(spObj, "dd"));
				map.put(JczqSPType.BQC_PF, CoreJSONUtils.getString(spObj, "da"));
				map.put(JczqSPType.BQC_FS, CoreJSONUtils.getString(spObj, "ah"));
				map.put(JczqSPType.BQC_FP, CoreJSONUtils.getString(spObj, "ad"));
				map.put(JczqSPType.BQC_FF, CoreJSONUtils.getString(spObj, "aa"));
			} else if (lotteryType.getValue() == LotteryType.JCZQ_SPF_WRQ.getValue()) {
				map.put(JczqSPType.SPF_WRQ_S, CoreJSONUtils.getString(spObj, "w"));
				map.put(JczqSPType.SPF_WRQ_P, CoreJSONUtils.getString(spObj, "d"));
				map.put(JczqSPType.SPF_WRQ_F, CoreJSONUtils.getString(spObj, "l"));
			} 
			
			item.setSpmap(map);
			jczqDynamicInstantSpItems.add(item);
		}
		return jczqDynamicInstantSpItems;
	}

	@Override
	public List<JczqDynamicInstantSpItem> fetchJczqSp(String phaseNo,
			LotteryType lotteryType) {
		return fetchJczqSp(phaseNo, lotteryType, null);
	}
}